/*    */ package com.zimbra.cs.datasource;
/*    */ 
/*    */ import com.zimbra.common.service.ServiceException;
/*    */ import com.zimbra.common.util.BufferStream;
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import com.zimbra.cs.mailbox.DeliveryContext;
/*    */ import com.zimbra.cs.mime.ParsedMessage;
/*    */ import com.zimbra.cs.store.Blob;
/*    */ import com.zimbra.cs.store.StoreManager;
/*    */ import java.io.IOException;
/*    */ import java.io.InputStream;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class MessageContent
/*    */ {
/*    */   private Blob blob;
/*    */   private byte[] data;
/*    */   
/*    */   public static MessageContent read(InputStream is, int size)
/*    */     throws IOException, ServiceException
/*    */   {
/* 35 */     MessageContent mb = new MessageContent();
/* 36 */     mb.readContent(is, size);
/* 37 */     return mb;
/*    */   }
/*    */   
/*    */   private void readContent(InputStream is, int sizeHint)
/*    */     throws IOException, ServiceException
/*    */   {
/* 43 */     if (sizeHint < StoreManager.getDiskStreamingThreshold()) {
/* 44 */       BufferStream bs = new BufferStream(sizeHint);
/*    */       
/* 46 */       long realSize = bs.readFrom(is);
/* 47 */       if (realSize != sizeHint) {}
/*    */       
/*    */ 
/* 50 */       this.data = bs.toByteArray();
/* 51 */       bs.close();
/*    */     } else {
/* 53 */       this.blob = StoreManager.getInstance().storeIncoming(is);
/*    */     }
/*    */   }
/*    */   
/*    */   public ParsedMessage getParsedMessage(Long receivedDate, boolean indexAttachments) throws IOException, ServiceException
/*    */   {
/* 59 */     if (this.data != null) {
/* 60 */       return this.data.length == 0 ? null : new ParsedMessage(this.data, receivedDate, indexAttachments);
/*    */     }
/* 62 */     return new ParsedMessage(this.blob, receivedDate, indexAttachments);
/*    */   }
/*    */   
/*    */   public int getSize()
/*    */   {
/* 67 */     if (this.data != null) {
/* 68 */       return this.data.length;
/*    */     }
/*    */     try {
/* 71 */       return (int)this.blob.getRawSize();
/*    */     } catch (IOException e) {
/* 73 */       ZimbraLog.datasource.error("Unable to determine size of %s.", this.blob.getPath(), e);
/*    */     }
/* 75 */     return 0;
/*    */   }
/*    */   
/*    */   public DeliveryContext getDeliveryContext() {
/* 79 */     DeliveryContext dc = new DeliveryContext();
/* 80 */     if (this.blob != null) {
/* 81 */       dc.setIncomingBlob(this.blob);
/*    */     }
/* 83 */     return dc;
/*    */   }
/*    */   
/*    */   public void cleanup() throws IOException {
/* 87 */     if (this.blob != null) {
/* 88 */       StoreManager.getInstance().delete(this.blob);
/* 89 */       this.blob = null;
/*    */     }
/*    */   }
/*    */   
/*    */   public void finalize() throws Throwable
/*    */   {
/*    */     try {
/* 96 */       cleanup();
/*    */     } finally {
/* 98 */       super.finalize();
/*    */     }
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/MessageContent.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */